home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2001 May / SGI Freeware 2001 May - Disc 2.iso / dist / fw_libxml.idb / usr / freeware / include / gnome-xml / parser.h.z / parser.h
C/C++ Source or Header  |  2001-04-12  |  15KB  |  427 lines

  1. /*
  2.  * parser.h : Interfaces, constants and types related to the XML parser.
  3.  *
  4.  * See Copyright for the status of this software.
  5.  *
  6.  * Daniel.Veillard@w3.org
  7.  */
  8.  
  9. #ifndef __XML_PARSER_H__
  10. #define __XML_PARSER_H__
  11.  
  12. #include "tree.h"
  13. #include "valid.h"
  14. #include "xmlIO.h"
  15. #include "entities.h"
  16.  
  17.  
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21.  
  22. /*
  23.  * Constants.
  24.  */
  25. #define XML_DEFAULT_VERSION    "1.0"
  26.  
  27. /**
  28.  * an xmlParserInput is an input flow for the XML processor.
  29.  * Each entity parsed is associated an xmlParserInput (except the
  30.  * few predefined ones). This is the case both for internal entities
  31.  * - in which case the flow is already completely in memory - or
  32.  * external entities - in which case we use the buf structure for
  33.  * progressive reading and I18N conversions to the internal UTF-8 format.
  34.  */
  35.  
  36. typedef void (* xmlParserInputDeallocate)(xmlChar *);
  37. typedef struct _xmlParserInput xmlParserInput;
  38. typedef xmlParserInput *xmlParserInputPtr;
  39. struct _xmlParserInput {
  40.     /* Input buffer */
  41.     xmlParserInputBufferPtr buf;      /* UTF-8 encoded buffer */
  42.  
  43.     const char *filename;             /* The file analyzed, if any */
  44.     const char *directory;            /* the directory/base of teh file */
  45.     const xmlChar *base;              /* Base of the array to parse */
  46.     const xmlChar *cur;               /* Current char being parsed */
  47.     int length;                       /* length if known */
  48.     int line;                         /* Current line */
  49.     int col;                          /* Current column */
  50.     int consumed;                     /* How many xmlChars already consumed */
  51.     xmlParserInputDeallocate free;    /* function to deallocate the base */
  52. };
  53.  
  54. /**
  55.  * the parser can be asked to collect Node informations, i.e. at what
  56.  * place in the file they were detected. 
  57.  * NOTE: This is off by default and not very well tested.
  58.  */
  59. typedef struct _xmlParserNodeInfo xmlParserNodeInfo;
  60. typedef xmlParserNodeInfo *xmlParserNodeInfoPtr;
  61.  
  62. struct _xmlParserNodeInfo {
  63.   const struct _xmlNode* node;
  64.   /* Position & line # that text that created the node begins & ends on */
  65.   unsigned long begin_pos;
  66.   unsigned long begin_line;
  67.   unsigned long end_pos;
  68.   unsigned long end_line;
  69. };
  70.  
  71. typedef struct _xmlParserNodeInfoSeq xmlParserNodeInfoSeq;
  72. typedef xmlParserNodeInfoSeq *xmlParserNodeInfoSeqPtr;
  73. struct _xmlParserNodeInfoSeq {
  74.   unsigned long maximum;
  75.   unsigned long length;
  76.   xmlParserNodeInfo* buffer;
  77. };
  78.  
  79. /**
  80.  * The parser is now working also as a state based parser
  81.  * The recursive one use the stagte info for entities processing
  82.  */
  83. typedef enum {
  84.     XML_PARSER_EOF = -1,    /* nothing is to be parsed */
  85.     XML_PARSER_START = 0,    /* nothing has been parsed */
  86.     XML_PARSER_MISC,        /* Misc* before int subset */
  87.     XML_PARSER_PI,        /* Whithin a processing instruction */
  88.     XML_PARSER_DTD,        /* within some DTD content */
  89.     XML_PARSER_PROLOG,        /* Misc* after internal subset */
  90.     XML_PARSER_COMMENT,        /* within a comment */
  91.     XML_PARSER_START_TAG,    /* within a start tag */
  92.     XML_PARSER_CONTENT,        /* within the content */
  93.     XML_PARSER_CDATA_SECTION,    /* within a CDATA section */
  94.     XML_PARSER_END_TAG,        /* within a closing tag */
  95.     XML_PARSER_ENTITY_DECL,    /* within an entity declaration */
  96.     XML_PARSER_ENTITY_VALUE,    /* within an entity value in a decl */
  97.     XML_PARSER_ATTRIBUTE_VALUE,    /* within an attribute value */
  98.     XML_PARSER_EPILOG         /* the Misc* after the last end tag */
  99. } xmlParserInputState;
  100.  
  101. /**
  102.  * The parser context.
  103.  * NOTE This doesn't completely defines the parser state, the (current ?)
  104.  *      design of the parser uses recursive function calls since this allow
  105.  *      and easy mapping from the production rules of the specification
  106.  *      to the actual code. The drawback is that the actual function call
  107.  *      also reflect the parser state. However most of the parsing routines
  108.  *      takes as the only argument the parser context pointer, so migrating
  109.  *      to a state based parser for progressive parsing shouldn't be too hard.
  110.  */
  111. typedef struct _xmlParserCtxt xmlParserCtxt;
  112. typedef xmlParserCtxt *xmlParserCtxtPtr;
  113. struct _xmlParserCtxt {
  114.     struct _xmlSAXHandler *sax;       /* The SAX handler */
  115.     void            *userData;        /* the document being built */
  116.     xmlDocPtr           myDoc;        /* the document being built */
  117.     int            wellFormed;        /* is the document well formed */
  118.     int       replaceEntities;        /* shall we replace entities ? */
  119.     const xmlChar       *version;        /* the XML version string */
  120.     const xmlChar      *encoding;        /* encoding, if any */
  121.     int            standalone;        /* standalone document */
  122.     int                  html;        /* are we parsing an HTML document */
  123.  
  124.     /* Input stream stack */
  125.     xmlParserInputPtr  input;         /* Current input stream */
  126.     int                inputNr;       /* Number of current input streams */
  127.     int                inputMax;      /* Max number of input streams */
  128.     xmlParserInputPtr *inputTab;      /* stack of inputs */
  129.  
  130.     /* Node analysis stack only used for DOM building */
  131.     xmlNodePtr         node;          /* Current parsed Node */
  132.     int                nodeNr;        /* Depth of the parsing stack */
  133.     int                nodeMax;       /* Max depth of the parsing stack */
  134.     xmlNodePtr        *nodeTab;       /* array of nodes */
  135.  
  136.     int record_info;                  /* Whether node info should be kept */
  137.     xmlParserNodeInfoSeq node_seq;    /* info about each node parsed */
  138.  
  139.     int errNo;                        /* error code */
  140.  
  141.     int     hasExternalSubset;        /* reference and external subset */
  142.     int             hasPErefs;        /* the internal subset has PE refs */
  143.     int              external;        /* are we parsing an external entity */
  144.  
  145.     int                 valid;        /* is the document valid */
  146.     int              validate;        /* shall we try to validate ? */
  147.     xmlValidCtxt        vctxt;        /* The validity context */
  148.  
  149.     xmlParserInputState instate;      /* current type of input */
  150.     int                 token;        /* next char look-ahead */    
  151.  
  152.     char           *directory;        /* the data directory */
  153.  
  154.     /* Node name stack only used for HTML parsing */
  155.     xmlChar           *name;          /* Current parsed Node */
  156.     int                nameNr;        /* Depth of the parsing stack */
  157.     int                nameMax;       /* Max depth of the parsing stack */
  158.     xmlChar *         *nameTab;       /* array of nodes */
  159.  
  160.     long               nbChars;       /* number of xmlChar processed */
  161.     long            checkIndex;       /* used by progressive parsing lookup */
  162.     int             keepBlanks;       /* ugly but ... */
  163. };
  164.  
  165. /**
  166.  * a SAX Locator.
  167.  */
  168. typedef struct _xmlSAXLocator xmlSAXLocator;
  169. typedef xmlSAXLocator *xmlSAXLocatorPtr;
  170. struct _xmlSAXLocator {
  171.     const xmlChar *(*getPublicId)(void *ctx);
  172.     const xmlChar *(*getSystemId)(void *ctx);
  173.     int (*getLineNumber)(void *ctx);
  174.     int (*getColumnNumber)(void *ctx);
  175. };
  176.  
  177. /**
  178.  * a SAX handler is bunch of callbacks called by the parser when processing
  179.  * of the input generate data or structure informations.
  180.  */
  181.  
  182. typedef xmlParserInputPtr (*resolveEntitySAXFunc) (void *ctx,
  183.                 const xmlChar *publicId, const xmlChar *systemId);
  184. typedef void (*internalSubsetSAXFunc) (void *ctx, const xmlChar *name,
  185.                             const xmlChar *ExternalID, const xmlChar *SystemID);
  186. typedef xmlEntityPtr (*getEntitySAXFunc) (void *ctx,
  187.                             const xmlChar *name);
  188. typedef xmlEntityPtr (*getParameterEntitySAXFunc) (void *ctx,
  189.                             const xmlChar *name);
  190. typedef void (*entityDeclSAXFunc) (void *ctx,
  191.                             const xmlChar *name, int type, const xmlChar *publicId,
  192.                 const xmlChar *systemId, xmlChar *content);
  193. typedef void (*notationDeclSAXFunc)(void *ctx, const xmlChar *name,
  194.                 const xmlChar *publicId, const xmlChar *systemId);
  195. typedef void (*attributeDeclSAXFunc)(void *ctx, const xmlChar *elem,
  196.                             const xmlChar *name, int type, int def,
  197.                 const xmlChar *defaultValue, xmlEnumerationPtr tree);
  198. typedef void (*elementDeclSAXFunc)(void *ctx, const xmlChar *name,
  199.                 int type, xmlElementContentPtr content);
  200. typedef void (*unparsedEntityDeclSAXFunc)(void *ctx,
  201.                             const xmlChar *name, const xmlChar *publicId,
  202.                 const xmlChar *systemId, const xmlChar *notationName);
  203. typedef void (*setDocumentLocatorSAXFunc) (void *ctx,
  204.                             xmlSAXLocatorPtr loc);
  205. typedef void (*startDocumentSAXFunc) (void *ctx);
  206. typedef void (*endDocumentSAXFunc) (void *ctx);
  207. typedef void (*startElementSAXFunc) (void *ctx, const xmlChar *name,
  208.                             const xmlChar **atts);
  209. typedef void (*endElementSAXFunc) (void *ctx, const xmlChar *name);
  210. typedef void (*attributeSAXFunc) (void *ctx, const xmlChar *name,
  211.                                   const xmlChar *value);
  212. typedef void (*referenceSAXFunc) (void *ctx, const xmlChar *name);
  213. typedef void (*charactersSAXFunc) (void *ctx, const xmlChar *ch,
  214.                     int len);
  215. typedef void (*ignorableWhitespaceSAXFunc) (void *ctx,
  216.                 const xmlChar *ch, int len);
  217. typedef void (*processingInstructionSAXFunc) (void *ctx,
  218.                             const xmlChar *target, const xmlChar *data);
  219. typedef void (*commentSAXFunc) (void *ctx, const xmlChar *value);
  220. typedef void (*cdataBlockSAXFunc) (void *ctx, const xmlChar *value, int len);
  221. typedef void (*warningSAXFunc) (void *ctx, const char *msg, ...);
  222. typedef void (*errorSAXFunc) (void *ctx, const char *msg, ...);
  223. typedef void (*fatalErrorSAXFunc) (void *ctx, const char *msg, ...);
  224. typedef int (*isStandaloneSAXFunc) (void *ctx);
  225. typedef int (*hasInternalSubsetSAXFunc) (void *ctx);
  226. typedef int (*hasExternalSubsetSAXFunc) (void *ctx);
  227.  
  228. typedef struct _xmlSAXHandler xmlSAXHandler;
  229. typedef xmlSAXHandler *xmlSAXHandlerPtr;
  230. struct _xmlSAXHandler {
  231.     internalSubsetSAXFunc internalSubset;
  232.     isStandaloneSAXFunc isStandalone;
  233.     hasInternalSubsetSAXFunc hasInternalSubset;
  234.     hasExternalSubsetSAXFunc hasExternalSubset;
  235.     resolveEntitySAXFunc resolveEntity;
  236.     getEntitySAXFunc getEntity;
  237.     entityDeclSAXFunc entityDecl;
  238.     notationDeclSAXFunc notationDecl;
  239.     attributeDeclSAXFunc attributeDecl;
  240.     elementDeclSAXFunc elementDecl;
  241.     unparsedEntityDeclSAXFunc unparsedEntityDecl;
  242.     setDocumentLocatorSAXFunc setDocumentLocator;
  243.     startDocumentSAXFunc startDocument;
  244.     endDocumentSAXFunc endDocument;
  245.     startElementSAXFunc startElement;
  246.     endElementSAXFunc endElement;
  247.     referenceSAXFunc reference;
  248.     charactersSAXFunc characters;
  249.     ignorableWhitespaceSAXFunc ignorableWhitespace;
  250.     processingInstructionSAXFunc processingInstruction;
  251.     commentSAXFunc comment;
  252.     warningSAXFunc warning;
  253.     errorSAXFunc error;
  254.     fatalErrorSAXFunc fatalError;
  255.     getParameterEntitySAXFunc getParameterEntity;
  256.     cdataBlockSAXFunc cdataBlock;
  257. };
  258.  
  259. /**
  260.  * External entity loaders types
  261.  */
  262. typedef xmlParserInputPtr (*xmlExternalEntityLoader)(const char *URL,
  263.                              const char *ID,
  264.                              xmlParserCtxtPtr context);
  265.  
  266. /**
  267.  * Global variables: just the default SAX interface tables and XML
  268.  * version infos.
  269.  */
  270. extern const char *xmlParserVersion;
  271.  
  272. extern xmlSAXLocator xmlDefaultSAXLocator;
  273. extern xmlSAXHandler xmlDefaultSAXHandler;
  274. extern xmlSAXHandler htmlDefaultSAXHandler;
  275.  
  276. /**
  277.  * entity substitution default behaviour.
  278.  */
  279.  
  280. extern int xmlSubstituteEntitiesDefaultValue;
  281.  
  282.  
  283.  
  284. /**
  285.  * Cleanup
  286.  */
  287. void        xmlCleanupParser    (void);
  288.  
  289. /**
  290.  * Input functions
  291.  */
  292. int        xmlParserInputRead    (xmlParserInputPtr in,
  293.                      int len);
  294. int        xmlParserInputGrow    (xmlParserInputPtr in,
  295.                      int len);
  296.  
  297. /**
  298.  * xmlChar handling
  299.  */
  300. xmlChar *    xmlStrdup        (const xmlChar *cur);
  301. xmlChar *    xmlStrndup        (const xmlChar *cur,
  302.                      int len);
  303. xmlChar *    xmlStrsub        (const xmlChar *str,
  304.                      int start,
  305.                      int len);
  306. const xmlChar *    xmlStrchr        (const xmlChar *str,
  307.                      xmlChar val);
  308. const xmlChar *    xmlStrstr        (const xmlChar *str,
  309.                      xmlChar *val);
  310. int        xmlStrcmp        (const xmlChar *str1,
  311.                      const xmlChar *str2);
  312. int        xmlStrncmp        (const xmlChar *str1,
  313.                      const xmlChar *str2,
  314.                      int len);
  315. int        xmlStrlen        (const xmlChar *str);
  316. xmlChar *    xmlStrcat        (xmlChar *cur,
  317.                      const xmlChar *add);
  318. xmlChar *    xmlStrncat        (xmlChar *cur,
  319.                      const xmlChar *add,
  320.                      int len);
  321.  
  322. /**
  323.  * Basic parsing Interfaces
  324.  */
  325. xmlDocPtr    xmlParseDoc        (xmlChar *cur);
  326. xmlDocPtr    xmlParseMemory        (char *buffer,
  327.                      int size);
  328. xmlDocPtr    xmlParseFile        (const char *filename);
  329. int        xmlSubstituteEntitiesDefault(int val);
  330. int        xmlKeepBlanksDefault    (int val);
  331.  
  332. /**
  333.  * Recovery mode 
  334.  */
  335. xmlDocPtr    xmlRecoverDoc        (xmlChar *cur);
  336. xmlDocPtr    xmlRecoverMemory    (char *buffer,
  337.                      int size);
  338. xmlDocPtr    xmlRecoverFile        (const char *filename);
  339.  
  340. /**
  341.  * Less common routines and SAX interfaces
  342.  */
  343. int        xmlParseDocument    (xmlParserCtxtPtr ctxt);
  344. xmlDocPtr    xmlSAXParseDoc        (xmlSAXHandlerPtr sax,
  345.                      xmlChar *cur,
  346.                      int recovery);
  347. int        xmlSAXUserParseFile    (xmlSAXHandlerPtr sax,
  348.                      void *user_data,
  349.                      const char *filename);
  350. int        xmlSAXUserParseMemory    (xmlSAXHandlerPtr sax,
  351.                      void *user_data,
  352.                      char *buffer,
  353.                      int size);
  354. xmlDocPtr    xmlSAXParseMemory    (xmlSAXHandlerPtr sax,
  355.                      char *buffer,
  356.                                         int size,
  357.                      int recovery);
  358. xmlDocPtr    xmlSAXParseFile        (xmlSAXHandlerPtr sax,
  359.                      const char *filename,
  360.                      int recovery);
  361. xmlDtdPtr    xmlParseDTD        (const xmlChar *ExternalID,
  362.                      const xmlChar *SystemID);
  363. xmlDtdPtr    xmlSAXParseDTD        (xmlSAXHandlerPtr sax,
  364.                      const xmlChar *ExternalID,
  365.                      const xmlChar *SystemID);
  366. /**
  367.  * SAX initialization routines
  368.  */
  369. void        xmlDefaultSAXHandlerInit(void);
  370. void        htmlDefaultSAXHandlerInit(void);
  371.  
  372. /**
  373.  * Parser contexts handling.
  374.  */
  375. void        xmlInitParserCtxt    (xmlParserCtxtPtr ctxt);
  376. void        xmlClearParserCtxt    (xmlParserCtxtPtr ctxt);
  377. void        xmlFreeParserCtxt    (xmlParserCtxtPtr ctxt);
  378. void        xmlSetupParserForBuffer    (xmlParserCtxtPtr ctxt,
  379.                      const xmlChar* buffer,
  380.                      const char* filename);
  381. xmlParserCtxtPtr xmlCreateDocParserCtxt    (xmlChar *cur);
  382.  
  383. /**
  384.  * Interfaces for the Push mode
  385.  */
  386. xmlParserCtxtPtr xmlCreatePushParserCtxt(xmlSAXHandlerPtr sax,
  387.                      void *user_data,
  388.                      const char *chunk,
  389.                      int size,
  390.                      const char *filename);
  391. int         xmlParseChunk        (xmlParserCtxtPtr ctxt,
  392.                      const char *chunk,
  393.                      int size,
  394.                      int terminate);
  395.  
  396. /**
  397.  * Node infos
  398.  */
  399. const xmlParserNodeInfo*
  400.         xmlParserFindNodeInfo    (const xmlParserCtxt* ctxt,
  401.                                                const xmlNode* node);
  402. void        xmlInitNodeInfoSeq    (xmlParserNodeInfoSeqPtr seq);
  403. void        xmlClearNodeInfoSeq    (xmlParserNodeInfoSeqPtr seq);
  404. unsigned long xmlParserFindNodeInfoIndex(const xmlParserNodeInfoSeq* seq,
  405.                                          const xmlNode* node);
  406. void        xmlParserAddNodeInfo    (xmlParserCtxtPtr ctxt,
  407.                      const xmlParserNodeInfo* info);
  408.  
  409. /*
  410.  * External entities handling actually implemented in xmlIO
  411.  */
  412.  
  413. void        xmlSetExternalEntityLoader(xmlExternalEntityLoader f);
  414. xmlExternalEntityLoader
  415.         xmlGetExternalEntityLoader(void);
  416. xmlParserInputPtr
  417.         xmlLoadExternalEntity    (const char *URL,
  418.                      const char *ID,
  419.                      xmlParserCtxtPtr context);
  420.  
  421. #ifdef __cplusplus
  422. }
  423. #endif
  424.  
  425. #endif /* __XML_PARSER_H__ */
  426.  
  427.